package com.swak.reactivex.transport;

import java.util.ArrayList;
import java.util.Collection;

import com.swak.exception.RemotingException;
import com.swak.exception.ServerException;
import com.swak.registry.URL;
import com.swak.utils.CollectionUtils;

public class HeaderExchangeServer implements ExchangeServer {

	private Server server;

	public HeaderExchangeServer(Server server) {
		this.server = server;
	}

	@Override
	public URL url() {
		return server.url();
	}

	@Override
	public void start() throws ServerException {
		server.start();
	}

	@Override
	public void close() throws ServerException {
		server.close();
	}

	@Override
	public void send(Object message, boolean sent) throws RemotingException {
		server.send(message, sent);
	}

	@Override
	public Collection<ExchangeChannel> getChannels() {
		Collection<ExchangeChannel> exchangeChannels = new ArrayList<ExchangeChannel>();
		Collection<Channel> channels = server.getChannels();
		if (CollectionUtils.isNotEmpty(channels)) {
			for (Channel channel : channels) {
				exchangeChannels.add(HeaderExchangeChannel.getOrAddChannel(channel));
			}
		}
		return exchangeChannels;
	}
}
